<template>
  <div class="userInfo-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>基本资料</span>
      </div>
      <div>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="InfoForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="登录名称">
            <el-input v-model="ruleForm.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="用户昵称" prop="nickname">
            <el-input v-model="ruleForm.nickname"></el-input>
          </el-form-item>
          <el-form-item label="用户邮箱" prop="email">
            <el-input v-model="ruleForm.email"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('InfoForm')">提交修改</el-button>
            <el-button @click="resetForm('InfoForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { updateUserInfoAPI } from '@/api'
import { mapActions } from 'vuex'
export default {
  data() {
    return {
      ruleForm: {
        username: this.$store.state.userInfoObj.username,
        nickname: '',
        email: ''
      },
      rules: {
        nickname: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '昵称必须是1-10位的非空字符串',
            trigger: ['blur', 'change']
          }
        ],
        email: [
          { required: true, message: '请输入用户邮箱', trigger: 'blur' },
          { type: 'email', message: '邮箱格式不正确', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    // 这里不能使用箭头函数，不然this会指向window
    id() {
      return this.$store.state.userInfoObj.id
    },
    nickname() {
      return this.ruleForm.nickname
    },
    email() {
      return this.ruleForm.email
    }
  },
  methods: {
    ...mapActions(['getUserInfo']),
    // 提交修改按钮
    submitForm(formName) {
      // eslint-disable-next-line
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { data } = await updateUserInfoAPI({
            id: this.id,
            nickname: this.nickname,
            email: this.email
          })
          // 清空输入框
          this.ruleForm.nickname = ''
          this.ruleForm.email = ''
          if (data.code === 0) {
            this.$message({
              showClose: true,
              message: '修改基本资料成功!',
              type: 'success'
            })
            // 更新用户信息成功，刷新 Vuex 中的数据
            this.getUserInfo()
          } else {
            this.$message({
              showClose: true,
              message: '修改基本资料失败!',
              type: 'error'
            })
          }
        } else {
          return false
        }
      })
    },
    // 重置按钮
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 400px;
}
</style>
